#include "mailloginwidget.h"
#include "loginwidget.h"

MailLoginWidget::MailLoginWidget(QWidget *parent)
    : QWidget{parent}
{
    // 设置基本属性
    this->setFixedSize(400, 350);
    this->setWindowTitle("登录");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 创建布局管理器
    _layout = new QGridLayout();
    _layout->setSpacing(10);
    _layout->setContentsMargins(50, 40, 50, 0);
    _layout->setAlignment(Qt::AlignTop);
    this->setLayout(_layout);

    // 创建标题
    _titleLabel = new QLabel();
    _titleLabel->setText("登录");
    _titleLabel->setAlignment(Qt::AlignCenter);
    _titleLabel->setFixedHeight(50);
    _titleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _titleLabel->setStyleSheet("QLabel { font-size: 40px; font-weight: 600; }");

    QString editStyle = "QLineEdit { border: none; border-radius: 10px; font-size: 20px; background-color: rgb(240, 240, 240); padding-left: 5px; }";
    // 创建邮箱输入框
    _mailNumberEdit = new QLineEdit();
    _mailNumberEdit->setFixedHeight(40);
    _mailNumberEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _mailNumberEdit->setPlaceholderText("邮箱");
    _mailNumberEdit->setStyleSheet(editStyle);

    // 创建验证码输入框
    _verifyCodeEdit = new QLineEdit();
    _verifyCodeEdit->setFixedHeight(40);
    _verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _verifyCodeEdit->setPlaceholderText("验证码");
    _verifyCodeEdit->setStyleSheet(editStyle);
    _verifyCodeEdit->setEchoMode(QLineEdit::Password);

    // 创建获取验证码按钮
    QString btnWhiteStyle = "QPushButton { border: none; border-radius: 10px; background-color: transparent; }";
    btnWhiteStyle += "QPushButton:pressed { background-color: rgb(240, 240, 240); }";
    _verifyCodeBtn = new QPushButton();
    _verifyCodeBtn->setFixedSize(100, 40);
    _verifyCodeBtn->setStyleSheet(btnWhiteStyle);
    _verifyCodeBtn->setText("发送验证码");

    // 创建登录按钮
    _submitBtn = new QPushButton();
    _submitBtn->setText("登录");
    _submitBtn->setFixedHeight(40);
    _submitBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    QString btnGreenStyle = "QPushButton { border: none; border-radius: 10px; background-color: rgb(44, 182, 61); color: rgb(255, 255, 255); font-size: 20px; }";
    btnGreenStyle += "QPushButton:pressed { background-color: rgb(240, 240, 240); }";
    _submitBtn->setStyleSheet(btnGreenStyle);

    // 创建切换到用户名登录/注册按钮
    _usernameModBtn = new QPushButton();
    _usernameModBtn->setFixedSize(100, 40);
    _usernameModBtn->setText("邮箱登录");
    _usernameModBtn->setStyleSheet(btnWhiteStyle);

    // 创建切换登录/注册按钮
    _switchModBtn = new QPushButton();
    _switchModBtn->setFixedSize(100, 40);
    _switchModBtn->setText("注册");
    _switchModBtn->setStyleSheet(btnWhiteStyle);

    // 添加到布局管理器中
    _layout->addWidget(_titleLabel, 0, 0, 1, 5);
    _layout->addWidget(_mailNumberEdit, 1, 0, 1, 5);
    _layout->addWidget(_verifyCodeEdit, 2, 0, 1, 4);
    _layout->addWidget(_verifyCodeBtn, 2, 4, 1, 1);
    _layout->addWidget(_submitBtn, 3, 0, 1, 5);
    _layout->addWidget(_usernameModBtn, 4, 0, 1, 1);
    _layout->addWidget(_switchModBtn, 4, 4, 1, 1);

    connect(_switchModBtn, &QPushButton::clicked, this, [=](){
        if (_isLoginMode)
        {
            this->setWindowTitle("注册");
            _titleLabel->setText("注册");
            _submitBtn->setText("注册");
            _usernameModBtn->setText("用户名注册");
            _switchModBtn->setText("登录");
        }
        else
        {
            this->setWindowTitle("登录");
            _titleLabel->setText("登录");
            _submitBtn->setText("登录");
            _usernameModBtn->setText("用户名登录");
            _switchModBtn->setText("注册");
        }
        _isLoginMode = !_isLoginMode;
    });

    connect(_usernameModBtn, &QPushButton::clicked, this, [=](){
        LoginWidget *loginWidget = new LoginWidget(nullptr);
        loginWidget->setIsLogin(_isLoginMode);
        loginWidget->show();
        this->close();
    });
}
